package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.Order;
import com.sunday.pinxinghui.entity.OrderItem;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/19.
 */

public class OrderProductAdapter extends BaseAdapter {

    private Context mContext;
    private List<OrderItem> dataSet;


    public OrderProductAdapter(Context context, List<OrderItem> datas) {
        mContext = context;
        dataSet = datas;
    }

    @Override
    public int getCount() {
        return dataSet.size();
    }

    @Override
    public Object getItem(int position) {
        return dataSet.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.order_product_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        OrderItem item = dataSet.get(position);
        if (!TextUtils.isEmpty(item.getProductImage())) {
            Glide.with(mContext)
                    .load(item.getProductImage())
                    .error(R.mipmap.ic_default)
                    .into(viewHolder.productImage);
        }
        viewHolder.productName.setText(item.getProductName());
        viewHolder.productParams.setText(item.getNormal());
        viewHolder.productPrice.setText(String.format("¥%s", item.getPrice().setScale(2, RoundingMode.HALF_UP)));
        viewHolder.productNum.setText("X" + item.getNum());

        switch (item.getType()) {
            case 1:
                //普通商品
                viewHolder.flagImg.setVisibility(View.GONE);
                break;
            case 2:
                //秒杀产品
                viewHolder.flagImg.setVisibility(View.VISIBLE);
                viewHolder.flagImg.setImageResource(R.mipmap.miaosha);
                break;
            case 3:
                //限时购商品
                viewHolder.flagImg.setVisibility(View.VISIBLE);
                viewHolder.flagImg.setImageResource(R.mipmap.qianggou);
                break;
        }


        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.product_image)
        ImageView productImage;
        @Bind(R.id.product_name)
        TextView productName;
        @Bind(R.id.product_price)
        TextView productPrice;
        @Bind(R.id.product_params)
        TextView productParams;
        @Bind(R.id.product_num)
        TextView productNum;
        @Bind(R.id.pro_layout)
        RelativeLayout proLayout;
        @Bind(R.id.flag_img)
        ImageView flagImg;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

